//
//  XYJToolViewController.m
//  徙游记-01
//
//  Created by hcios on 2017/6/22.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJToolViewController.h"
#import "ButtonView.h"
#import "TopView.h"
#import "PopDirctViewController.h"
#import "XYJChangeMoneyViewController.h"
#import "XYJLanguageViewController.h"

@interface XYJToolViewController ()<ButtonViewDelegate>{
    
    NSString * temp_minString;
    NSString * temp_maxString;
    NSString * current_timeString;
    NSString * nameString;
    NSString * cityMoneyDisplay;
    NSString * cityMoneyCode;
}

@property (nonatomic,strong) TopView *topView;

@property (nonatomic,strong) UIButton *directionBtn;

@property (nonatomic,copy) NSString *cityID;

@property (nonatomic,copy) NSString *cityName;

@end

@implementation XYJToolViewController

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"change" object:_cityID];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Btn" object:_cityName];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"工具";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpButtonView];
    [self setUpSubView];
    [self initNetWorking];
    
    //回调改变view的值
    //改变view
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeView:) name:@"change" object:_cityID];
    
    //btn
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeBtn:) name:@"Btn" object:_cityName];
    
}


-(void)setUpButtonView{
    
    ButtonView *buttonView = [[ButtonView alloc] initWithFrame:CGRectMake(0, 210, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 210 - 64 - 48)];
    buttonView.delegate = self;
    buttonView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:buttonView];
}

-(void)didClick:(id)sender{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn = sender;
    if (btn.tag == 50) {
        NSLog(@"语音翻译");
        XYJLanguageViewController *VC = [[XYJLanguageViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    if (btn.tag == 51) {
        NSLog(@"汇率转换");
        XYJChangeMoneyViewController *VC = [[XYJChangeMoneyViewController alloc] init];
        VC.cityMoneyCode = cityMoneyCode;
        VC.cityMoneyDisplay = cityMoneyDisplay;
        [self.navigationController pushViewController:VC animated:YES];
    }
    if (btn.tag == 52) {
        NSLog(@"消费记录");
    }
    if (btn.tag == 53) {
        NSLog(@"WiFi查询");
    }
}



-(void)changeBtn:(NSNotification *)not{
    //改变btn的值
    [_directionBtn setTitle:not.object forState:UIControlStateNormal];
    NSLog(@"%@",not.object);
}


-(void)changeView:(NSNotification *)not{
    
    NSString *urlString = [NSString stringWithFormat:@"http://chanyouji.com/api/wiki/destinations/infos/%@.json",not.object];
    NSDictionary *param = @{
                            @"page":@"1"
                            };
    
    NSString *imageURL = [NSString stringWithFormat:@"http://m.chanyouji.cn/destinations/%@-landscape.jpg",not.object];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
            temp_minString = numberToString(responseObject[@"temp_min"]);
            temp_maxString = numberToString(responseObject[@"temp_max"]);
            current_timeString = responseObject[@"current_time"];
            cityMoneyCode = responseObject[@"currency_code"];
            cityMoneyDisplay = responseObject[@"currency_display"];
        }
        
        //移除
        [_topView removeFromSuperview];
        //重新初始化
        _topView = [[TopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 210)];
        [_topView layoutSubviewsMinString:temp_minString withMaxString:temp_maxString andDaysString:current_timeString withURL:imageURL];
        [self.view addSubview:_topView];
        //刷新方法
        [_topView setNeedsLayout];
        [_topView layoutIfNeeded];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败");
    }];
}

-(void)initNetWorking{
    NSString *urlString = [NSString stringWithFormat:@"http://chanyouji.com/api/wiki/destinations/infos/140.json"];
    NSDictionary *param = @{
                            @"page":@"1"
                            };
    
    NSString *imageURL = @"http://m.chanyouji.cn/destinations/140-landscape.jpg";
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"网络请求成功");
        
        if (responseObject) {
            temp_minString = numberToString(responseObject[@"temp_min"]);
            temp_maxString = numberToString(responseObject[@"temp_max"]);
            current_timeString = responseObject[@"current_time"];
            cityMoneyCode = responseObject[@"currency_code"];
        }
        
        //初始化topView
        _topView = [[TopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 210)];
        _topView.backgroundColor = [UIColor lightGrayColor];
        [_topView layoutSubviewsMinString:temp_minString withMaxString:temp_maxString andDaysString:current_timeString withURL:imageURL];
        [self.view addSubview:_topView];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败");
    }];
}


-(void)setUpSubView{
    
    //btn
    _directionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_directionBtn setFrame:CGRectMake(SCREEN_WIDTH - 80, 12, 60, 40)];
    [_directionBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [_directionBtn setTitle:@"丽江" forState:UIControlStateNormal];
    _directionBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [_directionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_directionBtn setBackgroundColor:[UIColor clearColor]];
    [_directionBtn addTarget:self action:@selector(presentView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:_directionBtn];
    self.navigationItem.rightBarButtonItem = rightBtn;
}

-(void)presentView{
    PopDirctViewController *VC = [[PopDirctViewController alloc] init];
    [self presentViewController:VC animated:YES completion:nil];
}



@end
